<template>
  <div>
     <van-nav-bar
      title="修改地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-address-edit
    :address-info='item'
  :area-list="areaList"
  show-postal
  show-delete
  show-set-default
  show-search-result
  :search-result="searchResult"
  :area-columns-placeholder="['请选择', '请选择', '请选择']"
  @save="onSave"
  @delete="onDelete"
  @change-detail="onChangeDetail"
/>
  </div>
</template>

<script>
import { areaList } from '@vant/area-data'
import { Toast } from 'vant'
import { getOneAddress, updateAddress, deladdress } from '@api'
export default {
  data () {
    return {
      areaList,
      id: this.$route.query.id,
      searchResult: [],
      item: {}
    }
  },
  created () {
    this.getOneAddress()
  },
  methods: {
    getOneAddress  () {
      getOneAddress({ id: this.id }).then(res => {
        if (res.data.code === 200) {
          this.item = JSON.parse(res.data.data.content)
          this.item.id = res.data.data.id
        }
      })
    },
    onClickLeft () {
      history.go(-1)
    },
    onSave (item) {
      updateAddress(item).then(res => {
        if (res.data.code === 200) {
          Toast({
            type: 'success',
            message: '保存成功',
            onClose: () => {
              history.go(-1)
            }
          })
        }
      })
    },
    onDelete (item) {
      const obj = {}
      obj.params = {}
      obj.params.id = this.id
      deladdress(obj).then(res => {
        if (res.data.code === 200) {
          Toast({
            type: 'success',
            message: '删除成功',
            onClose: () => {
              history.go(-1)
            }
          })
        }
      })
    },
    onChangeDetail (val) {
      if (val) {
        this.searchResult = [
          {
            name: '黄龙万科中心',
            address: '杭州市西湖区'
          }
        ]
      } else {
        this.searchResult = []
      }
    }
  }

}
</script>

<style lang="scss" scoped>

</style>
